package com.leetcode.practice.self.ddd;

import com.leetcode.practice.self.entity.LogDomain;
import com.leetcode.practice.self.entity.UserDomain;
import com.leetcode.practice.self.listen.BaseEvent;
import com.leetcode.practice.self.listen.ListenerPublisher;
import com.leetcode.practice.self.listen.UserEvent;
import com.leetcode.practice.self.mapper.TestMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.UUID;

@Service
@Slf4j
public class TestService {

    @Autowired
    private TestMapper testMapper;

    @Autowired
    private ListenerPublisher listenerPublisher;

    @SneakyThrows
    public Long addTestUser(UserDomain userDomain) {
        log.info("开始添加用户");
        testMapper.insert(userDomain);
        UserEvent baseEvent = new UserEvent(UUID.randomUUID().toString(), LocalDateTime.now(),userDomain);
        listenerPublisher.pushBaseEvent(baseEvent);
        log.info("添加用户完成:{}",userDomain.getUserId());
        return userDomain.getUserId();
    }

    @EventListener
    public void finish(BaseEvent<LogDomain> logDomainBaseEvent){
        log.info("全部完成");
    }
}
